package com.demotest.controller;

import com.demotest.mapper.GirlsMapper;
import com.demotest.model.Girls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by liuzh on 2017/12/18.
 * 控制器
 */
@Controller
public class GirlsController {

    @Autowired
    GirlsMapper girlsMapper;

    @RequestMapping("/")
    public String home(){
        return "/login";
    }



    @RequestMapping("/index")
    public ModelAndView index(){

        String info = "A information from controller '/index' ...";

        ModelAndView modelAndView = new ModelAndView("index");

        modelAndView.addObject("info", info);

        return modelAndView;
    }




    @RequestMapping("/list")
    public ModelAndView showGirls(){

        ModelAndView modelAndView = new ModelAndView("index");

        List<Girls> girlsList = girlsMapper.showAll();

        modelAndView.addObject("girlsList", girlsList);

        return modelAndView;

    }



    @RequestMapping("/one")
    public ModelAndView showOne(){
        Girls girls = girlsMapper.selectById(1);

        ModelAndView modelAndView = new ModelAndView("index");

        modelAndView.addObject("girls",girls);

        System.out.println(girls);

        return modelAndView;
    }




    @RequestMapping("/one/{id}")
    public String showOne(@PathVariable(value = "id") int id, Model model){
        Girls girls = girlsMapper.selectById(id);

        model.addAttribute("girls",girls);

        System.out.println(girls);

        return "/index";
    }



    @RequestMapping("/login")
    public String login(){
        return "/login";
    }



    @RequestMapping("/sign")
    public String getName(String name, Model  model){
        model.addAttribute("name",name);
        System.out.println("name:" + name);
        return "/index";
    }




}
